package com.atguigu.cloud;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author: huahua
 * @Date: 2024/6/25 0025
 * @Time: 19:47
 * @Description:
 */
public class test01 {

    @Test
    public void test_1() {
        Integer[] array = {6, 9, 1, 7, 3, 4, 5, 1};
        Integer[] array1 = {6, 9, 1, 7, 3, 4, 5, 1};

        System.out.println("排序前:" + Arrays.toString(array));

        Arrays.sort(array1, (a, b) -> b - a);


        // bubbleSort(array);
        // selectSort(array);
        // insertSort(array);
        shellSort(array);

        System.out.println("排序后:" + Arrays.toString(array));
        System.out.println("方法排:" + Arrays.toString(array1));


    }

    public void bubbleSort(Integer[] array) {
        int arrayLength = array.length;
        for (int i = 0; i < arrayLength; i++) {
            for (int j = 0; j < arrayLength - i - 1; j++) {
                if (array[j] < array[j + 1]) {
                    int temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

    public void selectSort(Integer[] array) {
        int length = array.length;
        for (int i = 0; i < length; i++) {
            int zone = i;
            boolean b = false;
            for (int j = i; j < length; j++) {
                if (array[zone] < array[j]) {
                    zone = j;
                    b = true;
                }
            }
            if (b) {
                int temp = array[i];
                array[i] = array[zone];
                array[zone] = temp;
            }
        }
    }


    public void insertSort(Integer[] array) {
        int length = array.length;
        for (int i = 1; i < length; i++) {
            for (int j = i; j > 0; j--) {
                if (array[j] > array[j - 1]) {
                    int temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                }
            }
        }
    }

    public void shellSort(Integer[] array) {
        int length = array.length;
        for (int step = length / 2; step >= 1; step /= 2) {
            for (int i = 1; i < length; i++) {
                for (int j = i; j > 0; j--) {
                    if (array[j] > array[j - 1]) {
                        int temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
        }
    }
}
